/**
*      author: Y_Yao
*      created: 2024-11-04 11:26:11
**/

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 7;
const int M = 1e5 + 7;
int a[N][N];
int n,k,q;
int r,c,m;
char o;
int Big[M],Less[M];
int l[N],r[N];

bool check(int mid)
{
    for(int i = 1;i <= m;i++)
    {
        if(o[i] == '>')
        {
            if(a[mid][r[i]] <= c[i])
            {
                return true;
            }
            else continue;
        }
        else if(o[i] == '<')
        {
            if(a[mid][r[i]] >= c[i])
            {
                return false;
            }
            else continue;
        }
    }

    return true;
}

void solve()
{

    memset(Big,-INF,sizeof Big);
    memset(Less,INF,sizeof Less);
    cin >> n >> k >> q;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= k;j++)
        {
            int tmp = 1;
            cin >> tmp;
            a[i][j] = tmp;
            if(i >= 2)
            {
                a[i][j] = tmp | a[i-1][j];
            }
        }
    }

    // for(int i = 1;i <= n;i++)
    // {
    //     for(int j = 1;j <= k;j++)
    //     {
    //         cout << a[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    cin >> m;
    for(int i = 1;i <= m;i++)
    {
        cin >> r >> o >> c;
        if(o == '<')
        {
            Less[r] = min(Less[r],c);
        }
        else if(o == '>')
        {
            Big[r] = max(Big[r],c);
        }
    }
    //cout << r << o << c << endl;
    for
    {
        int l = 1,r = n;
        while(l + 1 < r)
        {
            int mid = (l + r) >> 1;
            if(check(mid)) l = mid;
            else r = mid;
        }
    }

    cout << l << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}